package com.earnings.okhttputils.utils.ui.activity.common;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.TurnRecordData;
import com.earnings.okhttputils.utils.bean.TurnRecordListData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpArrayCallback;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.utils.APPUserData;
import com.earnings.okhttputils.utils.utils.PayUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.earnings.R.id.phone;

/*
用户转账-设置金额
 */
public class TurnPriceActivity extends GodLeftHandBaseActivity implements GodOnClickListener {


    private EditText price;//金额

    private String paptypeid;
    private TextView change,info;//金额
    private PayUtil payUtil;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void init() {
        setTitle("转到招财汇宝账户");
setOnClickListener( this,R.id.sumbit,R.id.change );

        price=(EditText) findViewById(R.id.price);
        change=(TextView) findViewById(R.id.change);
        info=(TextView) findViewById(R.id.info);
        price.clearFocus();
        price.setFocusable(true);
        price.setFocusableInTouchMode(true);
        price.requestFocus();

        // getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        paptypeid= Constant.PAY_TYPE_BLANANCE;
        payUtil=new PayUtil(getContext(), price.getText().toString() ) {
            @Override
            public void onSuccess(String type,String msg) {
                info.setText( "使用"+ msg+"支付，");
                paptypeid=type;
            }
        };
        initData();loadDataForPhone();
    }


    public void initData(){
        setText( phone,bundleData.getString( "phone" ) );

        if(bundleData.containsKey( "price" )) price.setText( bundleData.getString( "price" ) );
    }

    public void saveInfo(){
        APPUserData<TurnRecordListData> appUserData=new APPUserData<TurnRecordListData>( this );
        TurnRecordData turnRecord=new TurnRecordData();
        List<TurnRecordData> records=new ArrayList<>();;
        turnRecord.setNickname( bundleData.getString( "nickname" )  );
        turnRecord.setMobile( bundleData.getString( "phone" )  );
        turnRecord.setHead_pic( bundleData.getString( "head" )  );
        TurnRecordListData data= appUserData.getModel( new TurnRecordListData() );
        if(data==null)data=new TurnRecordListData();
        else{
            if(data.getDatas()!=null)
                records=data.getDatas();
        }
        for (int i = 0; i <records.size(); i++) {
            if(records.get( i ).getMobile().equals( turnRecord.getMobile())){
                records.remove( i );
            }
        }
        records.add(0, turnRecord );
        data.setDatas( records );
        appUserData.doSave( data);
    }
public boolean checkPrice(){
    String money=price.getText().toString();
    if(money.equals("")){
        ToastUtils.showToast(getContext(),"请输入转账金额");
        return false;}

        return true;
}

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_turn_price;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int i = v.getId();
        if (i == R.id.sumbit) {
            if (!checkPrice()) return;

            new PayUtil( getContext(), price.getText().toString() ) {
                @Override
                public void onSuccess(String paytype, String info) {
                    saveInfo();
                    bundleData.putString( "title", "转账详情" );
                    bundleData.putString( "price", price.getText().toString() );
                    bundleData.putString( "info", "您给"+bundleData.getString( "nickname" )+"转账成功" );
                    bundleData.putString( "status", "转账成功" );
                    skipActivity( StatusPriceActivity.class );
                    finish();
                }
            }.showTransfer( bundleData.getString("phone"), paptypeid,getEditText(R.id.note).getText().toString() );


        } else if (i == R.id.change) {
            payUtil.showPayChoose();

        }
    }

    @Override
    public void OnClickListener(View view) {

    }

    public void loadDataForPhone() {
        showProgress();
        Map map = new HttpMap( getContext() );
        map.put( "mobile" , bundleData.getString( "phone" ) );
        OkHttpUtils.post().url( HttpUrl.USER_FIND_DATA_URL ).params( map ).build().execute(new HttpArrayCallback<TurnRecordData>( getContext() ) {
            @Override
            public void onSuccess(List<TurnRecordData> object, String msg) {
                if(object.size()!=0){


                    setText( R.id.nickname,object.get(0).getNickname());
                    setGlideImagView( R.id.head,object.get(0).getHead_pic() );

                } else {
                    setText( R.id.info,"未查找到该用户信息");
                    setTextColor( R.id.info, getContext().getResources().getColor( R.color.rad ) );

                }

                dismissProgress();
                setVisibility( R.id.info,View.VISIBLE );
                //  datas = (ArrayList<RecommendFriendsListData>) object.getList();
              /*  datas = (ArrayList<RecommendFriendsListData>) object.getList();
                initData();*/
            }

            @Override
            public void onFail(int code, String msg) {

                dismissProgress();
                ToastUtils.showToast( getContext(), msg );
            }


        } );


    }
}
